﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Electric;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit.MEP.Electric.Calculator;
using HYElectricCommon;

namespace YArchitech.Revit.MEP.Calculator
{
	public partial class DlgIllCalData : System.Windows.Forms.Form
	{
		public DlgIllCalData(UIApplication app)
		{
			this.m_application = app;
			this.m_document = app.ActiveUIDocument.Document;
			this.illCalDataSet = new IllCalDataSet();
			this.InitializeComponent();
			string fileName = HYElectricCommon.AssistFunc.GetAppRootPath() + "\\Data\\IlluminanceCal.db3";
			string text = Path.Combine(Product.WorkingLocation, "IlluminanceCal.db3");
			if (DataBaseTempFileOperate.IsExistFile(text))
			{
				if (!DataBaseTempFileOperate.IsSameVersion(fileName, text))
				{
					DataBaseTempFileOperate.CopyDBFile(fileName, Product.WorkingLocation);
				}
			}
			else
			{
				DataBaseTempFileOperate.CopyDBFile(fileName, Product.WorkingLocation);
			}
			SqLiteHelper.SqlLiteDatabase("Data Source=" + text + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10");
			this.dgvRoomData.Controls.Add(this.cmbCeilingFactor);
			this.dgvRoomData.Controls.Add(this.cmbWallUpFactor);
			this.dgvRoomData.Controls.Add(this.cmbWallDownFactor);
			this.dgvRoomData.Controls.Add(this.cmbGroundFactor);
			this.dgvRoomData.Controls.Add(this.btnIllAndPower);
			this.dgvRoomData.Refresh();
			this.InitCmbWallReflectionFactor();
			this.InitCmbGroundReflectionFactor();
			if (this.m_ToolTip == null)
			{
				this.m_ToolTip = new ToolTip();
				this.m_ToolTip.AutoPopDelay = 10000;
				this.m_ToolTip.InitialDelay = 500;
				this.m_ToolTip.ReshowDelay = 1000;
			}
			this.gbLamp.MouseMove += this.gbLamp_MouseMove;
			this.gbCal.MouseMove += this.gbCal_MouseMove;
		}

		private void tbFlux_MouseEnter(object sender, EventArgs e)
		{
			this.toolTip1.SetToolTip(this.tbFlux, "单位：lm");
		}

		private void tbLightPower_MouseEnter(object sender, EventArgs e)
		{
			this.toolTip1.SetToolTip(this.tbLightPower, "单位：W");
		}

		private void gbLamp_MouseMove(object sender, MouseEventArgs e)
		{
			this.AddLampTooltips(e, this.gbLamp);
		}

		private void gbCal_MouseMove(object sender, MouseEventArgs e)
		{
			this.AddCalTooltips(e, this.gbCal);
		}

		private void DlgIllCalData_Load(object sender, EventArgs e)
		{
			this.RefreshDataGridView();
		}

		private void AddLampTooltips(MouseEventArgs e, GroupBox groupBox)
		{
            System.Drawing.Point point = new System.Drawing.Point(e.X, e.Y);
			if (point.X >= this.tbBallast.Left && point.X <= this.tbBallast.Right && point.Y >= this.tbBallast.Top && point.Y <= this.tbBallast.Bottom)
			{
				if (!this.isHas)
				{
					this.isHas = true;
                    System.Drawing.Point point2 = new System.Drawing.Point((int)((double)this.tbBallast.Left + (double)this.tbBallast.Width / 2.0), this.tbBallast.Top + this.tbBallast.ClientSize.Height);
					this.m_ToolTip.Show("单位：W", groupBox, point2, 5000);
					return;
				}
			}
			else
			{
				if (this.m_ToolTip != null)
				{
					this.m_ToolTip.Hide(groupBox);
				}
				this.isHas = false;
			}
		}

		private void AddCalTooltips(MouseEventArgs e, GroupBox groupBox)
		{
            System.Drawing.Point point = new System.Drawing.Point(e.X, e.Y);
			if (point.X >= this.tbIllumination.Left && point.X <= this.tbIllumination.Right && point.Y >= this.tbIllumination.Top && point.Y <= this.tbIllumination.Bottom)
			{
				if (!this.isHas)
				{
					this.isHas = true;
                    System.Drawing.Point point2 = new System.Drawing.Point((int)((double)this.tbIllumination.Left + (double)this.tbIllumination.Width / 2.0), this.tbIllumination.Top + this.tbIllumination.ClientSize.Height);
					this.m_ToolTip.Show("单位：lx", groupBox, point2, 5000);
					return;
				}
			}
			else if (point.X >= this.tbPowerDensity.Left && point.X <= this.tbPowerDensity.Right && point.Y >= this.tbPowerDensity.Top && point.Y <= this.tbPowerDensity.Bottom)
			{
				if (!this.isHas)
				{
					this.isHas = true;
					System.Drawing.Point point3 = new System.Drawing.Point((int)((double)this.tbPowerDensity.Left + (double)this.tbPowerDensity.Width / 2.0), this.tbPowerDensity.Top + this.tbPowerDensity.ClientSize.Height);
					this.m_ToolTip.Show("单位：W/m²", groupBox, point3, 5000);
					return;
				}
			}
			else if (point.X >= this.tbConversionPowerDensity.Left && point.X <= this.tbConversionPowerDensity.Right && point.Y >= this.tbConversionPowerDensity.Top && point.Y <= this.tbConversionPowerDensity.Bottom)
			{
				if (!this.isHas)
				{
					this.isHas = true;
					System.Drawing.Point point4 = new System.Drawing.Point((int)((double)this.tbConversionPowerDensity.Left + (double)this.tbConversionPowerDensity.Width / 2.0), this.tbConversionPowerDensity.Top + this.tbConversionPowerDensity.ClientSize.Height);
					this.m_ToolTip.Show("单位：W/m²", groupBox, point4, 5000);
					return;
				}
			}
			else
			{
				if (this.m_ToolTip != null)
				{
					this.m_ToolTip.Hide(groupBox);
				}
				this.isHas = false;
			}
		}

		public void RefreshDataGridView()
		{
			this.isFind = false;
			this.SetBindingSource();
			for (int i = 0; i < this.illCalDataSet.listIllCalData.Count; i++)
			{
				IllCalData illCalData = this.illCalDataSet.listIllCalData.ElementAt(i);
				this.dgvRoomData.Rows[i].Cells[0].Value = ((illCalData.roomData.CalState == RoomCalState.eDefault) ? YArchitech.Electric.Resources.GetImage("CalFlagUnknown16") : YArchitech.Electric.Resources.GetImage("CalFlagOK16"));
				this.dgvRoomData.Rows[i].Cells[1].Value = illCalData.roomData.RoomIndex;
				this.dgvRoomData.Rows[i].Cells[2].Value = illCalData.roomData.RoomNumber;
				this.dgvRoomData.Rows[i].Cells[3].Value = illCalData.roomData.RoomName;
				this.dgvRoomData.Rows[i].Cells[4].Value = illCalData.roomData.RoomArea;
				this.dgvRoomData.Rows[i].Cells[5].Value = illCalData.roomData.RoomHeight;
				this.dgvRoomData.Rows[i].Cells[6].Value = illCalData.roomData.WorkHeight;
				this.dgvRoomData.Rows[i].Cells[7].Value = illCalData.roomData.InstallHeight;
				this.dgvRoomData.Rows[i].Cells[8].Value = illCalData.roomData.CeilingFactor;
				this.dgvRoomData.Rows[i].Cells[9].Value = illCalData.roomData.WallUpFactor;
				this.dgvRoomData.Rows[i].Cells[10].Value = illCalData.roomData.WallDownFactor;
				this.dgvRoomData.Rows[i].Cells[11].Value = illCalData.roomData.FloorFactor;
				this.dgvRoomData.Rows[i].Cells[12].Value = illCalData.roomData.WindowArea;
				this.dgvRoomData.Rows[i].Cells[13].Value = illCalData.roomData.WindowFactor;
				this.dgvRoomData.Rows[i].Cells[14].Value = illCalData.roomData.Lux;
				this.dgvRoomData.Rows[i].Cells[15].Value = illCalData.roomData.Lpd;
			}
			this.isFind = true;
		}

		public void SetBindingSource()
		{
			this.bindingSourceIllCalData.DataSource = typeof(IllCalData);
			this.dgvRoomData.DataSource = this.bindingSourceIllCalData;
			this.bindingSourceIllCalData.DataSource = this.illCalDataSet.listIllCalData;
		}

		public void InitMaintenanceFactor()
		{
			this.cmbMaintenanceFactor.Items.Clear();
			System.Data.DataTable dataTable = Database.ExecQuery("select DISTINCT [whxs], [Type] from [LampWHXS]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					string item = dataTable.Rows[i][0].ToString() + "|" + dataTable.Rows[i][1].ToString();
					this.cmbMaintenanceFactor.Items.Add(item);
				}
			}
			if (this.cmbMaintenanceFactor.Items.Count > 0)
			{
				this.cmbMaintenanceFactor.SelectedIndex = 0;
			}
		}

		public void InitLampType()
		{
			this.cmbLampType.Items.Clear();
			System.Data.DataTable dataTable = Database.ExecQuery("select [LampType] from [LampType]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					string item = dataTable.Rows[i][0].ToString();
					this.cmbLampType.Items.Add(item);
				}
			}
			if (this.cmbLampType.Items.Count > 1)
			{
				this.cmbLampType.SelectedIndex = 1;
			}
		}

		public void InitCommonlyUsedLampName()
		{
			this.cmbLampName.Items.Clear();
			System.Data.DataTable dataTable = Database.ExecQuery("select [Name],[IsStore] from [Lamp]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					if (Convert.ToBoolean(dataTable.Rows[i][1]))
					{
						string item = dataTable.Rows[i][0].ToString();
						this.cmbLampName.Items.Add(item);
					}
				}
			}
			if (this.cmbLampName.Items.Count > 0)
			{
				this.cmbLampName.SelectedIndex = 0;
			}
		}

		public void InitLampName()
		{
			this.cmbLampName.Items.Clear();
			int srcId = this.cmbLampType.SelectedIndex + 1;
			System.Data.DataTable dataTable = Database.ExecQuery("select [Name], [LampTypeID] from [Lamp]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					if (this.ContainsLampTypeId(srcId, dataTable.Rows[i][1].ToString()))
					{
						string item = dataTable.Rows[i][0].ToString();
						this.cmbLampName.Items.Add(item);
					}
				}
			}
			if (this.cmbLampName.Items.Count > 0)
			{
				this.cmbLampName.SelectedIndex = 0;
			}
		}

		private void InitLightType()
		{
			this.cmbLightType.Items.Clear();
			System.Data.DataTable dataTable = Database.ExecQuery("select [LightSourceType] from [LightSourceType]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					string item = dataTable.Rows[i][0].ToString();
					this.cmbLightType.Items.Add(item);
				}
			}
			if (this.cmbLightType.Items.Count > 0)
			{
				this.cmbLightType.SelectedIndex = 0;
			}
		}

		public void InitLightName()
		{
			this.cmbLightName.Items.Clear();
			int num = this.cmbLightType.SelectedIndex + 1;
			System.Data.DataTable dataTable = Database.ExecQuery("select DISTINCT [Name], [LightSourceTypeID] from [LightSource]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					if (num.ToString() == dataTable.Rows[i][1].ToString())
					{
						string item = dataTable.Rows[i][0].ToString();
						this.cmbLightName.Items.Add(item);
					}
				}
			}
			if (this.cmbLightName.Items.Count > 0)
			{
				this.cmbLightName.SelectedIndex = 0;
			}
		}

		private void InitLightModel()
		{
			this.cmbLightModel.Items.Clear();
			int lightSourceTypeIDByName = this.GetLightSourceTypeIDByName(this.cmbLightName.SelectedItem.ToString());
			string arg = this.cmbLightName.SelectedItem.ToString();
			System.Data.DataTable dataTable = Database.ExecQuery(string.Format("select [Model] from [LightSource] where [LightSourceTypeID]='{0}' and [Name]='{1}'", lightSourceTypeIDByName, arg));
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					string text = dataTable.Rows[i][0].ToString();
					if (!this.cmbLightModel.Items.Contains(text))
					{
						this.cmbLightModel.Items.Add(text);
					}
				}
			}
			if (this.cmbLightModel.Items.Count > 0)
			{
				this.cmbLightModel.SelectedIndex = 0;
			}
		}

		private int GetLightSourceTypeIDByName(string name)
		{
			System.Data.DataTable dataTable = Database.ExecQuery(string.Format("select [LightSourceTypeID] from [LightSource] where [Name]='{0}'", name));
			if (dataTable.Rows.Count > 0)
			{
				return Convert.ToInt32(dataTable.Rows[0][0]);
			}
			return -1;
		}

		private bool ContainsLampTypeId(int srcId, string sDes)
		{
			bool flag = false;
			for (int i = sDes.IndexOf(","); i > 0; i = sDes.IndexOf(","))
			{
				if (Convert.ToInt32(sDes.Substring(0, i)) == srcId)
				{
					flag = true;
					break;
				}
				sDes = sDes.Substring(i + 1);
			}
			if (!flag && Convert.ToInt32(sDes) == srcId)
			{
				flag = true;
			}
			return flag;
		}

		private void ResetLightSourceAndLampInfo()
		{
			try
			{
				if (this.cmbLampType.SelectedItem != null && this.cmbLampName.SelectedItem != null)
				{
					string text = this.cmbLampName.SelectedItem.ToString();
					this.SetCurrentLampParam(text);
					this.SetCurrentLightParam(text);
				}
			}
			catch (Exception)
			{
				MessageBox.Show("提取灯具或光源信息时失败,请确保所选的灯具类型中包含灯具及光源信息。", "盈建科提示");
			}
		}

		public void SetCurrentLampParam(string lampName)
		{
			System.Data.DataTable dataTable = Database.ExecQuery("select [LightSourceCount],[Model],[BallastCount],[BallastPower] from [Lamp] where [Name] ='" + lampName + "'");
			if (dataTable != null)
			{
				this.tbLightNum.Text = dataTable.Rows[0][0].ToString();
				this.tbLampStandard.Text = dataTable.Rows[0][1].ToString();
				this.tbBallast.Text = (Convert.ToDouble(dataTable.Rows[0][2].ToString()) * Convert.ToDouble(dataTable.Rows[0][3].ToString())).ToString();
			}
		}

		public void SetCurrentLightParam(string lampName)
		{
			System.Data.DataTable dataTable = Database.ExecQuery("select [DefaultLightSourceID] from [Lamp] where [Name] ='" + lampName + "'");
			if (dataTable != null && ((dataTable.Rows[0][0] == null) ? "" : dataTable.Rows[0][0].ToString()) != "")
			{
				int num = Convert.ToInt32(dataTable.Rows[0][0].ToString());
				System.Data.DataTable dataTable2 = Database.ExecQuery("select [LuminousFlux], [Power], [LightSourceTypeID],[Name],[Model] from [LightSource] where [ID] ='" + num + "'");
				if (dataTable2 != null)
				{
					string text = dataTable2.Rows[0][2].ToString();
					if (text != null)
					{
						System.Data.DataTable dataTable3 = Database.ExecQuery("select [LightSourceType] from [LightSourceType] where [ID] ='" + text + "'");
						if (dataTable3 != null && this.cmbLightType.Items.Count > 0 && this.cmbLightType.Items.Contains(dataTable3.Rows[0][0].ToString()))
						{
							this.cmbLightType.SelectedItem = dataTable3.Rows[0][0].ToString();
							this.InitLightName();
							this.InitLightModel();
							this.GetLightFlusPower();
						}
					}
					if (this.cmbLightName.Items.Count > 0 && this.cmbLightName.Items.Contains(dataTable2.Rows[0][3].ToString()))
					{
						this.cmbLightName.SelectedItem = dataTable2.Rows[0][3].ToString();
						this.InitLightModel();
						this.GetLightFlusPower();
					}
					if (this.cmbLightModel.Items.Count > 0 && this.cmbLightModel.Items.Contains(dataTable2.Rows[0][4].ToString()))
					{
						this.cmbLightModel.SelectedItem = dataTable2.Rows[0][4].ToString();
						this.GetLightFlusPower();
					}
					this.tbFlux.Text = dataTable2.Rows[0][0].ToString();
					this.tbLightPower.Text = dataTable2.Rows[0][1].ToString();
				}
			}
		}

		private void InitCmbWallReflectionFactor()
		{
			this.cmbCeilingFactor.Items.Clear();
			this.cmbWallUpFactor.Items.Clear();
			this.cmbWallDownFactor.Items.Clear();
			System.Data.DataTable dataTable = Database.ExecQuery("select [RefFactor], [Describe] from [CeilingRef]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					ReflectionFactorData reflectionFactorData = new ReflectionFactorData();
					reflectionFactorData.Describe = dataTable.Rows[i][1].ToString() + " :" + dataTable.Rows[i][0].ToString();
					reflectionFactorData.Value = Convert.ToDouble(dataTable.Rows[i][0].ToString());
					this.cmbCeilingFactor.Items.Add(reflectionFactorData);
					this.cmbWallUpFactor.Items.Add(reflectionFactorData);
					this.cmbWallDownFactor.Items.Add(reflectionFactorData);
				}
			}
		}

		private void InitCmbGroundReflectionFactor()
		{
			this.cmbGroundFactor.Items.Clear();
			System.Data.DataTable dataTable = Database.ExecQuery("select [RefFactor], [Describe] from [GroundRef]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					ReflectionFactorData reflectionFactorData = new ReflectionFactorData();
					reflectionFactorData.Describe = dataTable.Rows[i][1].ToString() + " :" + dataTable.Rows[i][0].ToString();
					reflectionFactorData.Value = Convert.ToDouble(dataTable.Rows[i][0].ToString());
					this.cmbGroundFactor.Items.Add(reflectionFactorData);
				}
			}
		}

		private void dgvRoomData_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			if (this.illCalDataSet.listIllCalData.Count <= 0)
			{
				return;
			}
			int rowIndex = e.RowIndex;
			this.illCalDataSet.activeIllCalData = this.illCalDataSet.listIllCalData.ElementAt(rowIndex);
			DataGridViewCell dataGridViewCell = this.dgvRoomData.Rows[e.RowIndex].Cells[e.ColumnIndex];
			if (e.ColumnIndex == 5)
			{
				if (Convert.ToDouble(dataGridViewCell.Value) < this.illCalDataSet.activeIllCalData.roomData.InstallHeight)
				{
					MessageBox.Show("房间高度必须大于或等于安装高度，请重新修改。", "盈建科提示");
					dataGridViewCell.Value = this.illCalDataSet.activeIllCalData.roomData.RoomHeight;
					return;
				}
				this.illCalDataSet.activeIllCalData.roomData.RoomHeight = Convert.ToDouble(dataGridViewCell.Value);
				if (this.isFind && !this.CalUseFactor())
				{
					this.tbUserFactor.Text = "0.00";
					return;
				}
			}
			else if (e.ColumnIndex == 6)
			{
				if (Convert.ToDouble(dataGridViewCell.Value) > this.illCalDataSet.activeIllCalData.roomData.InstallHeight)
				{
					MessageBox.Show("工作面高度必须小于或等于安装高度，请重新修改。", "盈建科提示");
					dataGridViewCell.Value = this.illCalDataSet.activeIllCalData.roomData.WorkHeight;
					return;
				}
				this.illCalDataSet.activeIllCalData.roomData.WorkHeight = Convert.ToDouble(dataGridViewCell.Value);
				if (this.isFind && !this.CalUseFactor())
				{
					this.tbUserFactor.Text = "0.00";
					return;
				}
			}
			else if (e.ColumnIndex == 7)
			{
				if (Convert.ToDouble(dataGridViewCell.Value) > this.illCalDataSet.activeIllCalData.roomData.RoomHeight || Convert.ToDouble(dataGridViewCell.Value) < this.illCalDataSet.activeIllCalData.roomData.WorkHeight)
				{
					MessageBox.Show("安装高度必须大于等于房间高度且小于等于工作面高度，请重新修改。", "盈建科提示");
					dataGridViewCell.Value = this.illCalDataSet.activeIllCalData.roomData.InstallHeight;
					return;
				}
				this.illCalDataSet.activeIllCalData.roomData.InstallHeight = Convert.ToDouble(dataGridViewCell.Value);
				if (this.isFind && !this.CalUseFactor())
				{
					this.tbUserFactor.Text = "0.00";
					return;
				}
			}
			else if (e.ColumnIndex == 12)
			{
				this.illCalDataSet.activeIllCalData.roomData.WindowArea = Convert.ToDouble(dataGridViewCell.Value);
				if (this.isFind && !this.CalUseFactor())
				{
					this.tbUserFactor.Text = "0.00";
					return;
				}
			}
			else if (e.ColumnIndex == 13)
			{
				this.illCalDataSet.activeIllCalData.roomData.WindowFactor = Convert.ToDouble(dataGridViewCell.Value);
				if (this.isFind && !this.CalUseFactor())
				{
					this.tbUserFactor.Text = "0.00";
				}
			}
		}

		private void dgvRoomData_CellEnter(object sender, DataGridViewCellEventArgs e)
		{
			if (this.illCalDataSet.listIllCalData.Count <= 0)
			{
				return;
			}
			int rowIndex = e.RowIndex;
			this.illCalDataSet.activeIllCalData = this.illCalDataSet.listIllCalData.ElementAt(rowIndex);
			DataGridViewCell dataGridViewCell = this.dgvRoomData.Rows[e.RowIndex].Cells[e.ColumnIndex];
			System.Drawing.Rectangle cellDisplayRectangle = this.dgvRoomData.GetCellDisplayRectangle(dataGridViewCell.ColumnIndex, dataGridViewCell.RowIndex, true);
			if (e.ColumnIndex == 8)
			{
				this.cmbCeilingFactor.Location = cellDisplayRectangle.Location;
				this.cmbCeilingFactor.Size = cellDisplayRectangle.Size;
				this.ComfirmComboBoxValue(this.cmbCeilingFactor, dataGridViewCell.Value.ToString());
				this.cmbCeilingFactor.Visible = true;
				this.UserFactorOperate(e, dataGridViewCell);
				return;
			}
			if (e.ColumnIndex == 9)
			{
				this.cmbWallUpFactor.Location = cellDisplayRectangle.Location;
				this.cmbWallUpFactor.Size = cellDisplayRectangle.Size;
				this.ComfirmComboBoxValue(this.cmbWallUpFactor, dataGridViewCell.Value.ToString());
				this.cmbWallUpFactor.Visible = true;
				this.UserFactorOperate(e, dataGridViewCell);
				return;
			}
			if (e.ColumnIndex == 10)
			{
				this.cmbWallDownFactor.Location = cellDisplayRectangle.Location;
				this.cmbWallDownFactor.Size = cellDisplayRectangle.Size;
				this.ComfirmComboBoxValue(this.cmbWallDownFactor, dataGridViewCell.Value.ToString());
				this.cmbWallDownFactor.Visible = true;
				this.UserFactorOperate(e, dataGridViewCell);
				return;
			}
			if (e.ColumnIndex == 11)
			{
				this.cmbGroundFactor.Location = cellDisplayRectangle.Location;
				this.cmbGroundFactor.Size = cellDisplayRectangle.Size;
				this.ComfirmComboBoxValue(this.cmbGroundFactor, dataGridViewCell.Value.ToString());
				this.cmbGroundFactor.Visible = true;
				this.UserFactorOperate(e, dataGridViewCell);
				return;
			}
			if (e.ColumnIndex == 14)
			{
				System.Drawing.Point location = cellDisplayRectangle.Location;
				location.X = location.X + cellDisplayRectangle.Width - this.btnIllAndPower.Width;
				this.btnIllAndPower.Location = location;
				this.btnIllAndPower.Visible = true;
				return;
			}
			if (e.ColumnIndex == 15)
			{
				System.Drawing.Point location2 = cellDisplayRectangle.Location;
				location2.X = location2.X + cellDisplayRectangle.Width - this.btnIllAndPower.Width;
				this.btnIllAndPower.Location = location2;
				this.btnIllAndPower.Visible = true;
			}
		}

		private void UserFactorOperate(DataGridViewCellEventArgs e, DataGridViewCell cell)
		{
			if (e.RowIndex == this.cellRow && e.ColumnIndex == this.cellCol)
			{
				this.isShow = true;
			}
			this.cellRow = e.RowIndex;
			this.cellCol = e.ColumnIndex;
			this.cellValue = cell.Value.ToString();
		}

		private void dgvRoomData_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			this.EditingControl.KeyPress -= this.EditingControl_KeyPress;
			this.EditingControl.TextChanged -= this.EditingControl_TextChanged;
			if (this.illCalDataSet.listIllCalData.Count <= 0)
			{
				return;
			}
			int rowIndex = e.RowIndex;
			this.illCalDataSet.activeIllCalData = this.illCalDataSet.listIllCalData.ElementAt(rowIndex);
			DataGridViewCell dataGridViewCell = this.dgvRoomData.Rows[e.RowIndex].Cells[e.ColumnIndex];
			if (e.ColumnIndex == 8)
			{
				this.CheckingForValid(rowIndex, dataGridViewCell);
				this.cmbCeilingFactor.Visible = false;
				if (this.isFind && this.isShow && this.cellValue != dataGridViewCell.Value.ToString() && !this.CalUseFactor())
				{
					this.tbUserFactor.Text = "0.00";
					return;
				}
			}
			else if (e.ColumnIndex == 9)
			{
				this.CheckingForValid(rowIndex, dataGridViewCell);
				this.cmbWallUpFactor.Visible = false;
				if (this.isFind && this.isShow && this.cellValue != dataGridViewCell.Value.ToString() && !this.CalUseFactor())
				{
					this.tbUserFactor.Text = "0.00";
					return;
				}
			}
			else if (e.ColumnIndex == 10)
			{
				this.CheckingForValid(rowIndex, dataGridViewCell);
				this.cmbWallDownFactor.Visible = false;
				if (this.isFind && this.isShow && this.cellValue != dataGridViewCell.Value.ToString() && !this.CalUseFactor())
				{
					this.tbUserFactor.Text = "0.00";
					return;
				}
			}
			else if (e.ColumnIndex == 11)
			{
				this.CheckingForValid(rowIndex, dataGridViewCell);
				this.cmbGroundFactor.Visible = false;
				if (this.isFind && this.isShow && this.cellValue != dataGridViewCell.Value.ToString() && !this.CalUseFactor())
				{
					this.tbUserFactor.Text = "0.00";
					return;
				}
			}
			else
			{
				if (e.ColumnIndex == 14)
				{
					this.btnIllAndPower.Visible = false;
					this.illCalDataSet.activeIllCalData.roomData.Lux = ((dataGridViewCell.Value == null || dataGridViewCell.Value.ToString() == "") ? 0.0 : Convert.ToDouble(dataGridViewCell.Value));
					return;
				}
				if (e.ColumnIndex == 15)
				{
					this.btnIllAndPower.Visible = false;
					this.illCalDataSet.activeIllCalData.roomData.Lpd = ((dataGridViewCell.Value == null || dataGridViewCell.Value.ToString() == "") ? 0.0 : Convert.ToDouble(dataGridViewCell.Value));
				}
			}
		}

        private void ComfirmComboBoxValue(YArchitech.Electric.Controls.YJKComboBox com, string cellValue)
		{
			com.SelectedIndex = -1;
			if (cellValue == null)
			{
				com.Text = "";
				return;
			}
			com.Text = cellValue;
		}

		private void CheckingForValid(int row, DataGridViewCell cell)
		{
			switch (cell.ColumnIndex)
			{
			case 8:
				if (!this.IsNumeric(cell.Value.ToString(), false, false))
				{
					cell.Value = this.illCalDataSet.listIllCalData.ElementAt(row).roomData.CeilingFactor.ToString();
					return;
				}
				this.illCalDataSet.listIllCalData.ElementAt(row).roomData.CeilingFactor = Convert.ToDouble(cell.Value);
				return;
			case 9:
				if (!this.IsNumeric(cell.Value.ToString(), false, false))
				{
					cell.Value = this.illCalDataSet.listIllCalData.ElementAt(row).roomData.WallUpFactor.ToString();
					return;
				}
				this.illCalDataSet.listIllCalData.ElementAt(row).roomData.WallUpFactor = Convert.ToDouble(cell.Value);
				return;
			case 10:
				if (!this.IsNumeric(cell.Value.ToString(), false, false))
				{
					cell.Value = this.illCalDataSet.listIllCalData.ElementAt(row).roomData.WallDownFactor.ToString();
					return;
				}
				this.illCalDataSet.listIllCalData.ElementAt(row).roomData.WallDownFactor = Convert.ToDouble(cell.Value);
				return;
			case 11:
				if (!this.IsNumeric(cell.Value.ToString(), false, false))
				{
					cell.Value = this.illCalDataSet.listIllCalData.ElementAt(row).roomData.FloorFactor.ToString();
					return;
				}
				this.illCalDataSet.listIllCalData.ElementAt(row).roomData.FloorFactor = Convert.ToDouble(cell.Value);
				return;
			default:
				return;
			}
		}

		private void cmbCeilingFactor_SelectionChangeCommitted(object sender, EventArgs e)
		{
			this.SetReflectionFactor(sender);
		}

		private void cmbWallUpFactor_SelectionChangeCommitted(object sender, EventArgs e)
		{
			this.SetReflectionFactor(sender);
		}

		private void cmbWallDownFactor_SelectionChangeCommitted(object sender, EventArgs e)
		{
			this.SetReflectionFactor(sender);
		}

		private void cmbGroundFactor_SelectionChangeCommitted(object sender, EventArgs e)
		{
			this.SetReflectionFactor(sender);
		}

		private void SetReflectionFactor(object sender)
		{
			YJKComboBox hycomboBox = sender as YJKComboBox;
			ReflectionFactorData reflectionFactorData = hycomboBox.SelectedItem as ReflectionFactorData;
			this.dgvRoomData.CurrentCell.Value = reflectionFactorData.Value.ToString();
			switch (this.dgvRoomData.CurrentCell.ColumnIndex)
			{
			case 8:
				this.illCalDataSet.listIllCalData.ElementAt(this.dgvRoomData.CurrentRow.Index).roomData.CeilingFactor = reflectionFactorData.Value;
				break;
			case 9:
				this.illCalDataSet.listIllCalData.ElementAt(this.dgvRoomData.CurrentRow.Index).roomData.WallUpFactor = reflectionFactorData.Value;
				break;
			case 10:
				this.illCalDataSet.listIllCalData.ElementAt(this.dgvRoomData.CurrentRow.Index).roomData.WallDownFactor = reflectionFactorData.Value;
				break;
			case 11:
				this.illCalDataSet.listIllCalData.ElementAt(this.dgvRoomData.CurrentRow.Index).roomData.FloorFactor = reflectionFactorData.Value;
				break;
			}
			hycomboBox.Visible = false;
			this.isShow = false;
		}

		private void cmbCeilingFactor_TextChanged(object sender, EventArgs e)
		{
			this.ExecuteTextChanged(sender);
		}

		private void cmbWallUpFactor_TextChanged(object sender, EventArgs e)
		{
			this.ExecuteTextChanged(sender);
		}

		private void cmbWallDownFactor_TextChanged(object sender, EventArgs e)
		{
			this.ExecuteTextChanged(sender);
		}

		private void cmbGroundFactor_TextChanged(object sender, EventArgs e)
		{
			this.ExecuteTextChanged(sender);
		}

		private void ExecuteTextChanged(object sender)
		{
			string text = (sender as YJKComboBox).Text;
			string[] array = new string[0];
			array = text.Split(new char[]
			{
				':'
			});
			if (array.Length != 0)
			{
				string numStr = array.Last<string>();
				if (this.IsNumeric(numStr, false, false))
				{
					this.dgvRoomData.CurrentCell.Value = array.Last<string>();
				}
			}
		}

		private void cmbCeilingFactor_KeyPress(object sender, KeyPressEventArgs e)
		{
			char keyChar = e.KeyChar;
			if (keyChar != '\b' && (keyChar < '0' || keyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private void dgvRoomData_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			int columnIndex = this.dgvRoomData.CurrentCell.ColumnIndex;
			this.EditingControl = (DataGridViewTextBoxEditingControl)e.Control;
			this.EditingControl.KeyPress += this.EditingControl_KeyPress;
			this.EditingControl.TextChanged += this.EditingControl_TextChanged;
		}

		private void dgvRoomData_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			int columnIndex = e.ColumnIndex;
			System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
			if (this.dgvRoomData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
			{
				textBox.Text = "";
			}
			else
			{
				textBox.Text = this.dgvRoomData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
			}
			this.NotepastText(textBox, ref this.pastText);
		}

		private void EditingControl_TextChanged(object sender, EventArgs e)
		{
			if (this.IsNumeric(this.EditingControl.Text, true, true))
			{
				this.NotepastText(this.EditingControl, ref this.pastText);
				return;
			}
			this.RestoreText(this.EditingControl, this.pastText);
		}

		private void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckKeyIn(e, true);
		}

		private bool IsNumeric(string NumStr, bool MayEmpty, bool ContainDot)
		{
			if ((NumStr ?? "") == "")
			{
				return MayEmpty;
			}
			if (ContainDot)
			{
				int num = NumStr.IndexOf('.');
				if (num >= 0)
				{
					num = NumStr.IndexOf('.', num + 1);
					if (num >= 0)
					{
						return false;
					}
				}
			}
			else if (NumStr.IndexOf('.') >= 0)
			{
				return false;
			}
			char[] array = NumStr.ToCharArray();
			for (int i = 0; i <= array.GetUpperBound(0); i++)
			{
				if (!char.IsDigit(array[i]))
				{
					if (array[i] != '.')
					{
						return false;
					}
					if (!ContainDot)
					{
						return false;
					}
				}
			}
			return !ContainDot || NumStr.Replace('.', '\0').Length > 0;
		}

		private void CheckKeyIn(KeyPressEventArgs Key, bool AllowDot)
		{
			if (char.IsControl(Key.KeyChar))
			{
				return;
			}
			if (char.IsDigit(Key.KeyChar))
			{
				return;
			}
			if (Key.KeyChar == '.')
			{
				if (!AllowDot)
				{
					Key.KeyChar = '\0';
				}
				return;
			}
			Key.KeyChar = '\0';
		}

		private void NotepastText(DataGridViewTextBoxEditingControl txtFrom, ref DlgIllCalData.strupastText NoteTo)
		{
			if (txtFrom.Text == "" || txtFrom.Text == null)
			{
				txtFrom.Text = "0";
			}
			NoteTo.Text = txtFrom.Text;
			NoteTo.SelStart = txtFrom.SelectionStart;
			NoteTo.SelLength = txtFrom.SelectionLength;
		}

        private void NotepastText(System.Windows.Forms.TextBox txtFrom, ref DlgIllCalData.strupastText NoteTo)
		{
			if (txtFrom.Text == "" || txtFrom.Text == null)
			{
				txtFrom.Text = "0";
			}
			NoteTo.Text = txtFrom.Text;
			NoteTo.SelStart = txtFrom.SelectionStart;
			NoteTo.SelLength = txtFrom.SelectionLength;
		}

		private void RestoreText(DataGridViewTextBoxEditingControl txtRestoreTo, DlgIllCalData.strupastText TextFrom)
		{
			txtRestoreTo.Text = TextFrom.Text;
			txtRestoreTo.SelectionStart = TextFrom.SelStart;
			txtRestoreTo.SelectionLength = TextFrom.SelLength;
		}

		private void dgvRoomData_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
		{
			this.cmbCeilingFactor.Visible = false;
			this.cmbWallUpFactor.Visible = false;
			this.cmbWallDownFactor.Visible = false;
			this.cmbGroundFactor.Visible = false;
			this.btnIllAndPower.Visible = false;
		}

		private void dgvRoomData_Scroll(object sender, ScrollEventArgs e)
		{
			this.cmbCeilingFactor.Visible = false;
			this.cmbWallUpFactor.Visible = false;
			this.cmbWallDownFactor.Visible = false;
			this.cmbGroundFactor.Visible = false;
			this.btnIllAndPower.Visible = false;
		}

		private void tbLightPower_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			this.Validata(textBox.Text.Trim(), e);
		}

		private void tbFlux_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			this.Validata(textBox.Text.Trim(), e);
		}

		private void tbUserFactor_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			this.Validata(textBox.Text.Trim(), e);
		}

		private void tbBallast_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			this.Validata(textBox.Text.Trim(), e);
		}

		private void Validata(string txt, KeyPressEventArgs e)
		{
			if (e.KeyChar == ' ')
			{
				e.Handled = true;
				return;
			}
			if (e.KeyChar == '\b')
			{
				return;
			}
			double num = 0.0;
			if (!double.TryParse(txt + e.KeyChar.ToString(), out num))
			{
				e.Handled = true;
				return;
			}
			if (num >= 0.0)
			{
				e.Handled = false;
				return;
			}
			e.Handled = true;
		}

		public void SaveCurrentLampParam()
		{
			try
			{
				if (this.cmbLampType.SelectedItem != null)
				{
					this.illCalDataSet.activeIllCalData.lampData.Type = this.cmbLampType.SelectedItem.ToString();
				}
				if (this.cmbLampName.SelectedItem != null)
				{
					this.illCalDataSet.activeIllCalData.lampData.Name = this.cmbLampName.SelectedItem.ToString();
				}
				if (this.tbLampStandard.Text.Length > 0)
				{
					this.illCalDataSet.activeIllCalData.lampData.Spec = this.tbLampStandard.Text;
				}
				if (this.tbMaintenanceFactor.Text.Length > 0)
				{
					this.illCalDataSet.activeIllCalData.lampData.MaintenanceFactor = Convert.ToDouble(this.tbMaintenanceFactor.Text);
				}
				else
				{
					this.illCalDataSet.activeIllCalData.lampData.MaintenanceFactor = 0.0;
				}
				if (this.tbUserFactor.Text.Length > 0)
				{
					this.illCalDataSet.activeIllCalData.lampData.UseFactor = Convert.ToDouble(this.tbUserFactor.Text);
				}
				else
				{
					this.illCalDataSet.activeIllCalData.lampData.UseFactor = 0.0;
				}
				if (this.tbBallast.Text.Length > 0)
				{
					this.illCalDataSet.activeIllCalData.lampData.BallastPower = Convert.ToDouble(this.tbBallast.Text);
				}
				else
				{
					this.illCalDataSet.activeIllCalData.lampData.BallastPower = 0.0;
				}
			}
			catch (Exception)
			{
				MessageBox.Show("提取灯具或光源信息时失败,请确保所选的灯具类型中包含灯具及光源信息。", "盈建科提示");
			}
		}

		private int getLampId(string name)
		{
			System.Data.DataTable dataTable = Database.ExecQuery(string.Format("select [ID] from [Lamp] where [Name]='{0}'", name));
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				return Convert.ToInt32(dataTable.Rows[0][0]);
			}
			return -1;
		}

		private double GetMaintenanceFactorByDescribe(string sDescribe)
		{
			double result = 0.0;
			if (sDescribe.Length <= 0)
			{
				return result;
			}
			int length = sDescribe.IndexOf("|");
			return Convert.ToDouble(sDescribe.Substring(0, length));
		}

		private void dgvRoomData_SelectionChanged(object sender, EventArgs e)
		{
			if (this.dgvRoomData.Rows.Count <= 0 || this.illCalDataSet.listIllCalData.Count <= 0)
			{
				return;
			}
			if (this.dgvRoomData.SelectedRows.Count == 1)
			{
				DlgIllCalMain dlgIllCalMain = (DlgIllCalMain)base.Parent.Parent.Parent;
				dlgIllCalMain.tsbMultiModify.Enabled = false;
				dlgIllCalMain.tsbDelRoomTag.Enabled = true;
				int index = this.dgvRoomData.CurrentRow.Index;
				this.illCalDataSet.activeIllCalData = this.illCalDataSet.listIllCalData.ElementAt(index);
				this.RefreshCurrentLampParam();
				this.RefreshCurrentLightSourceParam();
				this.CalUseFactorForBatchModify();
				this.RefreshCurrentCalData();
				return;
			}
			if (this.dgvRoomData.SelectedRows.Count > 1)
			{
				DlgIllCalMain dlgIllCalMain2 = (DlgIllCalMain)base.Parent.Parent.Parent;
				dlgIllCalMain2.tsbMultiModify.Enabled = true;
				dlgIllCalMain2.tsbDelRoomTag.Enabled = false;
				return;
			}
			DlgIllCalMain dlgIllCalMain3 = (DlgIllCalMain)base.Parent.Parent.Parent;
			dlgIllCalMain3.tsbMultiModify.Enabled = false;
			dlgIllCalMain3.tsbDelRoomTag.Enabled = false;
		}

		private void tbLightPower_TextChanged(object sender, EventArgs e)
		{
			if (this.tbLightPower.Text.Length > 0)
			{
				this.illCalDataSet.activeIllCalData.lightSourceData.Power = Convert.ToDouble(this.tbLightPower.Text);
				return;
			}
			this.illCalDataSet.activeIllCalData.lightSourceData.Power = 0.0;
		}

		private void tbFlux_TextChanged(object sender, EventArgs e)
		{
			if (this.tbFlux.Text.Length > 0)
			{
				this.illCalDataSet.activeIllCalData.lightSourceData.LuminousFlux = Convert.ToDouble(this.tbFlux.Text);
				return;
			}
			this.illCalDataSet.activeIllCalData.lightSourceData.LuminousFlux = 0.0;
		}

		private void tbUserFactor_TextChanged(object sender, EventArgs e)
		{
			if (this.tbUserFactor.Text.Length > 0)
			{
				this.illCalDataSet.activeIllCalData.lampData.UseFactor = Convert.ToDouble(this.tbUserFactor.Text);
				return;
			}
			this.illCalDataSet.activeIllCalData.lampData.UseFactor = 0.0;
		}

		private void tbBallast_TextChanged(object sender, EventArgs e)
		{
			if (this.tbBallast.Text.Length > 0)
			{
				this.illCalDataSet.activeIllCalData.lampData.BallastPower = Convert.ToDouble(this.tbBallast.Text);
				return;
			}
			this.illCalDataSet.activeIllCalData.lampData.BallastPower = 0.0;
		}

		public void SaveCurrentLightSourceParam()
		{
			try
			{
				if (this.cmbLightType.SelectedItem != null)
				{
					this.illCalDataSet.activeIllCalData.lightSourceData.Type = this.cmbLightType.SelectedItem.ToString();
				}
				if (this.cmbLightName.SelectedItem != null)
				{
					this.illCalDataSet.activeIllCalData.lightSourceData.Name = this.cmbLightName.SelectedItem.ToString();
				}
				if (this.cmbLightModel.SelectedItem != null)
				{
					this.illCalDataSet.activeIllCalData.lightSourceData.Model = this.cmbLightModel.SelectedItem.ToString();
				}
				if (this.tbLightNum.Text.Length > 0)
				{
					this.illCalDataSet.activeIllCalData.lightSourceData.Number = Convert.ToInt32(this.tbLightNum.Text);
				}
				if (this.tbLightPower.Text.Length > 0)
				{
					this.illCalDataSet.activeIllCalData.lightSourceData.Power = Convert.ToDouble(this.tbLightPower.Text);
				}
				else
				{
					this.illCalDataSet.activeIllCalData.lightSourceData.Power = 0.0;
				}
				if (this.tbFlux.Text.Length > 0)
				{
					this.illCalDataSet.activeIllCalData.lightSourceData.LuminousFlux = Convert.ToDouble(this.tbFlux.Text);
				}
				else
				{
					this.illCalDataSet.activeIllCalData.lightSourceData.LuminousFlux = 0.0;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public void RefreshCurrentLampParam()
		{
			try
			{
				object itemByYJKComboBox = this.GetItemByYJKComboBox(this.cmbLampType, this.illCalDataSet.activeIllCalData.lampData.Type);
				if (itemByYJKComboBox != null)
				{
					this.cmbLampType.SelectedItem = itemByYJKComboBox;
					if (this.cmbLampType.SelectedItem.ToString() == "常用灯具")
					{
						this.InitCommonlyUsedLampName();
					}
					else
					{
						this.InitLampName();
					}
					this.InitLightType();
				}
				else
				{
					this.cmbLampType.SelectedIndex = -1;
					this.cmbLampName.Items.Clear();
					this.cmbLightType.Items.Clear();
				}
				object itemByYJKComboBox2 = this.GetItemByYJKComboBox(this.cmbLampName, this.illCalDataSet.activeIllCalData.lampData.Name);
				if (itemByYJKComboBox2 != null)
				{
					this.cmbLampName.SelectedItem = itemByYJKComboBox2;
				}
				else
				{
					this.cmbLampName.SelectedIndex = -1;
				}
				string spec = this.illCalDataSet.activeIllCalData.lampData.Spec;
				if (!string.IsNullOrEmpty(spec))
				{
					this.tbLampStandard.Text = spec;
				}
				else
				{
					this.tbLampStandard.Text = "";
				}
				this.tbMaintenanceFactor.Text = this.illCalDataSet.activeIllCalData.lampData.MaintenanceFactor.ToString();
				this.tbUserFactor.Text = this.illCalDataSet.activeIllCalData.lampData.UseFactor.ToString("0.00");
				this.tbBallast.Text = this.illCalDataSet.activeIllCalData.lampData.BallastPower.ToString();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

        private object GetItemByYJKComboBox(YArchitech.Electric.Controls.YJKComboBox combobox, string value)
		{
			foreach (object obj in combobox.Items)
			{
				if (obj.ToString() == value)
				{
					return obj;
				}
			}
			return null;
		}

		public void RefreshCurrentLightSourceParam()
		{
			try
			{
				object itemByYJKComboBox = this.GetItemByYJKComboBox(this.cmbLightType, this.illCalDataSet.activeIllCalData.lightSourceData.Type);
				if (itemByYJKComboBox != null)
				{
					this.cmbLightType.SelectedItem = itemByYJKComboBox;
					this.InitLightName();
				}
				else
				{
					this.cmbLightType.SelectedIndex = -1;
					this.cmbLightName.Items.Clear();
				}
				object itemByYJKComboBox2 = this.GetItemByYJKComboBox(this.cmbLightName, this.illCalDataSet.activeIllCalData.lightSourceData.Name);
				if (itemByYJKComboBox2 != null)
				{
					this.cmbLightName.SelectedItem = itemByYJKComboBox2;
					this.InitLightModel();
				}
				else
				{
					this.cmbLightName.SelectedIndex = -1;
					this.cmbLightModel.Items.Clear();
				}
				object itemByYJKComboBox3 = this.GetItemByYJKComboBox(this.cmbLightModel, this.illCalDataSet.activeIllCalData.lightSourceData.Model);
				if (itemByYJKComboBox3 != null)
				{
					this.cmbLightModel.SelectedItem = itemByYJKComboBox3;
				}
				else
				{
					this.cmbLightModel.SelectedIndex = -1;
				}
				this.tbLightNum.Text = this.illCalDataSet.activeIllCalData.lightSourceData.Number.ToString();
				this.tbLightPower.Text = this.illCalDataSet.activeIllCalData.lightSourceData.Power.ToString();
				this.tbFlux.Text = this.illCalDataSet.activeIllCalData.lightSourceData.LuminousFlux.ToString();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public void RefreshCurrentCalData()
		{
			try
			{
				CalResultData calResultData = this.illCalDataSet.activeIllCalData.calResultData;
				this.tbLampNum.Text = calResultData.LampNumber.ToString();
				this.tbIllumination.Text = calResultData.Lux.ToString("0.00");
				this.tbPowerDensity.Text = calResultData.Lpd.ToString("0.00");
				this.tbConversionPowerDensity.Text = calResultData.LpdEx.ToString("0.00");
				CheckedState checkedState = calResultData.LuxChecked;
				if (checkedState == CheckedState.eMeet)
				{
					this.tbCheckingillumination.Text = "满足";
				}
				else if (checkedState == CheckedState.eNoMeet)
				{
					this.tbCheckingillumination.Text = "不满足";
				}
				else if (checkedState == CheckedState.eUnknown)
				{
					this.tbCheckingillumination.Text = "未校核";
				}
				checkedState = calResultData.LpdChecked;
				if (checkedState == CheckedState.eMeet)
				{
					this.tbCheckingPowerDensity.Text = "满足";
				}
				else if (checkedState == CheckedState.eNoMeet)
				{
					this.tbCheckingPowerDensity.Text = "不满足";
				}
				else if (checkedState == CheckedState.eUnknown)
				{
					this.tbCheckingPowerDensity.Text = "未校核";
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void cmbLampType_SelectionChangeCommitted(object sender, EventArgs e)
		{
			if (this.cmbLampType.SelectedItem.ToString() == "常用灯具")
			{
				this.InitCommonlyUsedLampName();
			}
			else
			{
				this.InitLampName();
			}
			if (this.cmbLampName.Items.Count == 0)
			{
				MessageBox.Show("提取灯具名称时失败,请确保所选的灯具类型中包含灯具。", "盈建科提示");
				return;
			}
			this.InitLightType();
			this.InitLightName();
			this.InitLightModel();
			this.LampNameInfo();
			if (this.cmbLampType.SelectedItem != null && this.cmbLampName.SelectedItem != null && !this.CalUseFactor())
			{
				this.tbUserFactor.Text = "0.00";
			}
		}

		private void cmbLampName_SelectionChangeCommitted(object sender, EventArgs e)
		{
			this.LampNameInfo();
			if (this.cmbLampType.SelectedItem != null && this.cmbLampName.SelectedItem != null && !this.CalUseFactor())
			{
				this.tbUserFactor.Text = "0.00";
			}
		}

		private void LampNameInfo()
		{
			this.ResetLightSourceAndLampInfo();
			this.SaveCurrentLampParam();
			this.SaveCurrentLightSourceParam();
		}

		private void cmbLightType_SelectionChangeCommitted(object sender, EventArgs e)
		{
			this.InitLightName();
			this.InitLightModel();
			this.GetLightFlusPower();
			this.SaveCurrentLightSourceParam();
		}

		private void cmbLightName_SelectionChangeCommitted(object sender, EventArgs e)
		{
			this.InitLightModel();
			this.GetLightFlusPower();
			this.SaveCurrentLightSourceParam();
		}

		private void cmbLightModel_SelectionChangeCommitted(object sender, EventArgs e)
		{
			this.GetLightFlusPower();
			this.SaveCurrentLightSourceParam();
		}

		private void GetLightFlusPower()
		{
			int lightSourceTypeIDByName = this.GetLightSourceTypeIDByName(this.cmbLightName.SelectedItem.ToString());
			string arg = this.cmbLightName.SelectedItem.ToString();
			string arg2 = this.cmbLightModel.SelectedItem.ToString();
			System.Data.DataTable dataTable = Database.ExecQuery(string.Format("select [LuminousFlux],[Power] from [LightSource] where [LightSourceTypeID]='{0}' and [Name]='{1}' and [Model]='{2}'", lightSourceTypeIDByName, arg, arg2));
			if (dataTable != null)
			{
				this.tbFlux.Text = dataTable.Rows[0][0].ToString();
				this.tbLightPower.Text = dataTable.Rows[0][1].ToString();
			}
		}

		private void btnIllAndPower_Click(object sender, EventArgs e)
		{
			int rowIndex = this.dgvRoomData.CurrentCell.RowIndex;
			using (DlgSelIllAndPower dlgSelIllAndPower = new DlgSelIllAndPower("设计室"))
			{
				if (dlgSelIllAndPower.ShowDialog() == DialogResult.OK)
				{
					try
					{
						this.dgvRoomData.Rows[rowIndex].Cells[14].Value = dlgSelIllAndPower.m_IllAndPowerData.IllStandard;
						this.dgvRoomData.Rows[rowIndex].Cells[15].Value = dlgSelIllAndPower.m_IllAndPowerData.NowPD;
						this.illCalDataSet.activeIllCalData.roomData.Lux = dlgSelIllAndPower.m_IllAndPowerData.IllStandard;
						this.illCalDataSet.activeIllCalData.roomData.Lpd = dlgSelIllAndPower.m_IllAndPowerData.NowPD;
						this.btnIllAndPower.Parent.Focus();
						this.dgvRoomData.RefreshEdit();
					}
					catch (Exception)
					{
					}
				}
				string str = Path.Combine(Product.WorkingLocation, "IlluminanceCal.db3");
				SqLiteHelper.SqlLiteDatabase("Data Source=" + str + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10");
			}
		}

		public bool CalUseFactor()
		{
			if (this.cmbLampType.SelectedItem == null || this.cmbLampName.SelectedItem == null)
			{
				this.RefreshCalData();
				return false;
			}
			double num = this.GetUserFactor();
			if (num != 0.0)
			{
				num += 0.005;
				this.tbUserFactor.Text = num.ToString("0.00");
				this.illCalDataSet.activeIllCalData.lampData.UseFactor = num;
				return true;
			}
			this.RefreshCalData();
			return false;
		}

		public bool CalUseFactorForBatchModify()
		{
			if (this.cmbLampType.SelectedItem == null || this.cmbLampName.SelectedItem == null)
			{
				this.RefreshCalData();
				return false;
			}
			double num = this.GetUserFactorForBatchModify();
			if (num != 0.0)
			{
				num += 0.005;
				this.tbUserFactor.Text = num.ToString("0.00");
				this.illCalDataSet.activeIllCalData.lampData.UseFactor = num;
				return true;
			}
			this.RefreshCalData();
			return false;
		}

		private double GetUserFactorForBatchModify()
		{
			try
			{
				string str = this.cmbLampName.SelectedItem.ToString();
				System.Data.DataTable dataTable = Database.ExecQuery("select [UseFactorTable] from [Lamp] where [Name] ='" + str + "'");
				if (dataTable != null)
				{
					dataTable.Rows[0][0].ToString();
				}
				FindUseFactor findUseFactor = new FindUseFactor(dataTable.Rows[0][0].ToString(), this.illCalDataSet.activeIllCalData.roomData, this.m_application.Application.VersionNumber);
				double result = 0.0;
				if (findUseFactor.GetUseFactor(ref result))
				{
					return result;
				}
			}
			catch
			{
			}
			return 0.0;
		}

		private double GetUserFactor()
		{
			string tableName = "";
			string str = this.cmbLampName.SelectedItem.ToString();
			System.Data.DataTable dataTable = Database.ExecQuery("select [UseFactorTable] from [Lamp] where [Name] ='" + str + "'");
			if (dataTable != null)
			{
				tableName = dataTable.Rows[0][0].ToString();
			}
			FindUseFactor findUseFactor = new FindUseFactor(tableName, this.illCalDataSet.activeIllCalData.roomData, this.m_application.Application.VersionNumber);
			try
			{
				double result = 0.0;
				if (findUseFactor.GetUseFactor(ref result))
				{
					return result;
				}
				if (!string.IsNullOrEmpty(findUseFactor.ErrorDescription))
				{
					MessageBox.Show(findUseFactor.ErrorDescription, "盈建科提示");
				}
				else
				{
					MessageBox.Show("获取灯具的利用系数值异常", "盈建科提示");
				}
			}
			catch
			{
				if (!string.IsNullOrEmpty(findUseFactor.ErrorDescription))
				{
					MessageBox.Show(findUseFactor.ErrorDescription, "盈建科提示");
				}
				else
				{
					MessageBox.Show("获取灯具的利用系数值异常", "盈建科提示");
				}
			}
			return 0.0;
		}

		private void btnCal_Click(object sender, EventArgs e)
		{
			this.Cal();
		}

		private void Cal()
		{
			if (this.dgvRoomData.RowCount <= 0)
			{
				return;
			}
			if (this.cmbLampType.SelectedItem == null || this.cmbLampName.SelectedItem == null)
			{
				MessageBox.Show("灯具数据不满足要求，请检查灯具数据。", "盈建科提示");
				this.RefreshCalData();
				return;
			}
			if (this.tbUserFactor.Text.Length == 0 || Convert.ToDouble(this.tbUserFactor.Text) <= 0.0)
			{
				MessageBox.Show("利用系数不满足要求，请检查利用系数数据。", "盈建科提示");
				this.RefreshCalData();
				return;
			}
			int index = this.dgvRoomData.CurrentRow.Index;
			this.illCalDataSet.activeIllCalData = this.illCalDataSet.listIllCalData.ElementAt(index);
			string text;
			if (!this.CalVerify(this.illCalDataSet.activeIllCalData, out text))
			{
				MessageBox.Show(text, "盈建科提示");
				this.RefreshCalData();
				return;
			}
			IllCalculate illCalculate = new IllCalculate(this.illCalDataSet.activeIllCalData.roomData, this.illCalDataSet.activeIllCalData.lampData, this.illCalDataSet.activeIllCalData.lightSourceData, IllCalType.eIllCal, 0);
			illCalculate.Cal();
			CalResultData resultData = illCalculate.ResultData;
			this.illCalDataSet.activeIllCalData.calResultData.LampNumber = resultData.LampNumber;
			this.illCalDataSet.activeIllCalData.calResultData.Lux = resultData.Lux;
			this.illCalDataSet.activeIllCalData.calResultData.LuxChecked = resultData.LuxChecked;
			this.illCalDataSet.activeIllCalData.calResultData.Lpd = resultData.Lpd;
			this.illCalDataSet.activeIllCalData.calResultData.LpdEx = resultData.LpdEx;
			this.illCalDataSet.activeIllCalData.calResultData.LpdChecked = resultData.LpdChecked;
			this.RefreshCurrentCalData();
			if (this.illCalDataSet.activeIllCalData.calResultData.LuxChecked == CheckedState.eMeet && this.illCalDataSet.activeIllCalData.calResultData.LpdChecked == CheckedState.eMeet)
			{
				this.illCalDataSet.activeIllCalData.roomData.CalState = RoomCalState.eCaled;
				this.dgvRoomData.Rows[index].Cells[0].Value = YArchitech.Electric.Resources.GetImage("CalFlagOK16");
			}
			else
			{
				this.illCalDataSet.activeIllCalData.roomData.CalState = RoomCalState.eDefault;
				this.dgvRoomData.Rows[index].Cells[0].Value = YArchitech.Electric.Resources.GetImage("CalFlagUnknown16");
			}
			this.dgvRoomData.Rows[index].Selected = true;
			this.dgvRoomData.CurrentCell = this.dgvRoomData.Rows[index].Cells[0];
		}

		private void RefreshCalData()
		{
			this.illCalDataSet.activeIllCalData.calResultData = new CalResultData();
			this.RefreshCurrentCalData();
			int index = this.dgvRoomData.CurrentRow.Index;
			this.illCalDataSet.activeIllCalData.roomData.CalState = RoomCalState.eDefault;
			this.dgvRoomData.Rows[index].Cells[0].Value = YArchitech.Electric.Resources.GetImage("CalFlagUnknown16");
		}

		private bool CalVerify(IllCalData illCalData, out string error)
		{
			error = "";
			if (illCalData.roomData.RoomArea <= 0.0)
			{
				error = "房间数据不满足要求，请检查空间数据。";
				return false;
			}
			LampData lampData = illCalData.lampData;
			if (lampData.MaintenanceFactor <= 0.0 || lampData.UseFactor <= 0.0 || this.tbMaintenanceFactor.Text == "" || this.tbBallast.Text == "")
			{
				error = "灯具数据不满足要求，请检查灯具数据。";
				return false;
			}
			LightSourceData lightSourceData = illCalData.lightSourceData;
			if (lightSourceData.Power <= 0.0 || lightSourceData.LuminousFlux <= 0.0 || lightSourceData.Number <= 0 || this.tbLightPower.Text == "" || this.tbFlux.Text == "" || this.tbLightNum.Text == "")
			{
				error = "光源数据不满足要求，请检查光源数据。";
				return false;
			}
			return true;
		}

		private void dgvRoomData_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
		{
			if (sender is DataGridView && e.RowIndex > -1 && e.ColumnIndex == 0)
			{
				DataGridView dataGridView = sender as DataGridView;
				this.illCalDataSet.activeIllCalData = this.illCalDataSet.listIllCalData.ElementAt(e.RowIndex);
				if (this.illCalDataSet.activeIllCalData.roomData.CalState == RoomCalState.eDefault)
				{
					dataGridView[e.ColumnIndex, e.RowIndex].ToolTipText = "未计算";
					return;
				}
				dataGridView[e.ColumnIndex, e.RowIndex].ToolTipText = "已计算";
			}
		}

		private void cmbMaintenanceFactor_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.tbMaintenanceFactor.Text = this.GetMaintenanceFactorByDescribe(this.cmbMaintenanceFactor.Items[this.cmbMaintenanceFactor.SelectedIndex].ToString()).ToString();
			if (this.tbMaintenanceFactor.Text.Length > 0)
			{
				this.illCalDataSet.activeIllCalData.lampData.MaintenanceFactor = Convert.ToDouble(this.tbMaintenanceFactor.Text);
			}
		}

		private void tbMaintenanceFactor_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			this.Validata(textBox.Text.Trim(), e);
		}

		private void tbMaintenanceFactor_TextChanged(object sender, EventArgs e)
		{
			if (this.tbMaintenanceFactor.Text.Length > 0)
			{
				this.illCalDataSet.activeIllCalData.lampData.MaintenanceFactor = Convert.ToDouble(this.tbMaintenanceFactor.Text);
				return;
			}
			this.illCalDataSet.activeIllCalData.lampData.MaintenanceFactor = 0.0;
		}

		private void tbLightNum_TextChanged(object sender, EventArgs e)
		{
			if (this.tbLightNum.Text.Length > 0)
			{
				this.illCalDataSet.activeIllCalData.lightSourceData.Number = Convert.ToInt32(this.tbLightNum.Text);
				return;
			}
			this.illCalDataSet.activeIllCalData.lightSourceData.Number = 0;
		}

		private void tbLightNum_KeyPress(object sender, KeyPressEventArgs e)
		{
			DlgIllCalData.IntegerValidation(e);
		}

		private void btnAdjustingIllumination_Click(object sender, EventArgs e)
		{
			if (this.dgvRoomData.RowCount <= 0)
			{
				return;
			}
			if (this.tbLampNum.Text.Length == 0)
			{
				MessageBox.Show("计算套数不能为空。", "盈建科提示");
				return;
			}
			int lampNum = Convert.ToInt32(this.tbLampNum.Text.Trim());
			if (this.cmbLampType.SelectedItem == null || this.cmbLampName.SelectedItem == null)
			{
				MessageBox.Show("灯具数据不满足要求，请检查灯具数据。", "盈建科提示");
				return;
			}
			if (this.tbUserFactor.Text.Length == 0 || Convert.ToDouble(this.tbUserFactor.Text) <= 0.0)
			{
				MessageBox.Show("利用系数不满足要求，请检查利用系数数据。", "盈建科提示");
				return;
			}
			int index = this.dgvRoomData.CurrentRow.Index;
			this.illCalDataSet.activeIllCalData = this.illCalDataSet.listIllCalData.ElementAt(index);
			string text;
			if (!this.CalVerify(this.illCalDataSet.activeIllCalData, out text))
			{
				MessageBox.Show(text, "盈建科提示");
				return;
			}
			IllCalculate illCalculate = new IllCalculate(this.illCalDataSet.activeIllCalData.roomData, this.illCalDataSet.activeIllCalData.lampData, this.illCalDataSet.activeIllCalData.lightSourceData, IllCalType.eIllCal, 0);
			illCalculate.Cal(lampNum);
			CalResultData resultData = illCalculate.ResultData;
			this.illCalDataSet.activeIllCalData.calResultData.LampNumber = resultData.LampNumber;
			this.illCalDataSet.activeIllCalData.calResultData.Lux = resultData.Lux;
			this.illCalDataSet.activeIllCalData.calResultData.LuxChecked = resultData.LuxChecked;
			this.illCalDataSet.activeIllCalData.calResultData.Lpd = resultData.Lpd;
			this.illCalDataSet.activeIllCalData.calResultData.LpdEx = resultData.LpdEx;
			this.illCalDataSet.activeIllCalData.calResultData.LpdChecked = resultData.LpdChecked;
			this.RefreshCurrentCalData();
			if (this.illCalDataSet.activeIllCalData.calResultData.LuxChecked == CheckedState.eMeet && this.illCalDataSet.activeIllCalData.calResultData.LpdChecked == CheckedState.eMeet)
			{
				this.illCalDataSet.activeIllCalData.roomData.CalState = RoomCalState.eCaled;
				this.dgvRoomData.Rows[index].Cells[0].Value = YArchitech.Electric.Resources.GetImage("CalFlagOK16");
			}
			else
			{
				this.illCalDataSet.activeIllCalData.roomData.CalState = RoomCalState.eDefault;
				this.dgvRoomData.Rows[index].Cells[0].Value = YArchitech.Electric.Resources.GetImage("CalFlagUnknown16");
			}
			this.dgvRoomData.Rows[index].Selected = true;
			this.dgvRoomData.CurrentCell = this.dgvRoomData.Rows[index].Cells[0];
		}

		private void tbLampNum_TextChanged(object sender, EventArgs e)
		{
			if (this.tbLampNum.Text.Length > 0)
			{
				this.illCalDataSet.activeIllCalData.calResultData.LampNumber = Convert.ToInt32(this.tbLampNum.Text);
				return;
			}
			this.illCalDataSet.activeIllCalData.calResultData.LampNumber = 0;
		}

		private void tbLampNum_KeyPress(object sender, KeyPressEventArgs e)
		{
			DlgIllCalData.IntegerValidation(e);
		}

		private static void IntegerValidation(KeyPressEventArgs e)
		{
			char keyChar = e.KeyChar;
			if (keyChar != '\b' && (keyChar < '0' || keyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private UIApplication m_application;

		private Document m_document;

		public IllCalDataSet illCalDataSet;

		private DataGridViewTextBoxEditingControl EditingControl = new DataGridViewTextBoxEditingControl();

		public bool isFind;

		private ToolTip m_ToolTip;

		private bool isHas;

		private string cellValue;

		private bool isShow = true;

		private int cellRow;

		private int cellCol;

		private DlgIllCalData.strupastText pastText;

		private struct strupastText
		{
			public strupastText(string txt, int Start, int Length)
			{
				this.Text = txt;
				this.SelStart = Start;
				this.SelLength = Length;
			}

			public string Text;

			public int SelStart;

			public int SelLength;
		}
	}
}
